
using System;

namespace WindowsFormsApplication1
{


	public class BeatParameters
	{
		public int PreviousRRInterval;
		public int RRInterval;
		public int QRSDuration;
		public int Maximum;
		public int Minimum;
		public int Area;
		public int Integral;
		public int Offset;
		public int Onset;
		public int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11;
		
		public bool fit;
		private int count = 0;
	
		
		public BeatParameters (int[] amostras,int Dtime)
		{
			CalculaParametros(amostras,Dtime);
		}
		
		public BeatParameters (int valor)
		{
			x1=x2=x3=x4=x5=x6=x7=x8=x9=x10=x11=PreviousRRInterval=RRInterval=QRSDuration=Maximum=Minimum=Area=Integral=Offset=Onset = valor;
		}
		public BeatParameters (int[] amostras)
		{
		//	CalculaParametros(amostras);
		}
		
		public BeatParameters ()
		{
			
		}
		
		public int detectOffSet ( int[] amostras,int Dtime)
		{
			
			while(amostras[Dtime]>=amostras[Dtime+1]){
			      Dtime++;
			}
			return Dtime;
		}
		
		public int detectOnSet (int[] amostras, int Dtime)
		{
			while(amostras[Dtime]<amostras[Dtime-1])
			      Dtime--;
			while(amostras[Dtime]>=amostras[Dtime-1])
				Dtime--;
			return Dtime;
		}

		
		
		public void CalculaParametros(int[] amostras, int d1){
			
			int integral=0, area=0, min=1000000, max = 0;
			int sample=0;
			int baseSample = amostras[2*(d1)/5];
		//	int baseSample = 0;
			for(int i=3*(d1)/5;i<d1+2*(amostras.Length-d1)/5;i++){
					sample = amostras[i] - baseSample;
				if(sample<min)
					min = sample;
				if(sample>max)
					max = sample;
				integral+=(sample);
				if(sample<0)
					sample*=-1;
				area+=sample;
			}
			int onIndex = detectOffSet(amostras,d1);
			int offIndex = detectOnSet(amostras,d1);
			
			QRSDuration = onIndex - offIndex;
			Offset=amostras[offIndex]-baseSample;
			Onset=amostras[onIndex]-baseSample;
			Area = area;
			Integral = integral;
			Maximum = max;
			Minimum = min;
			RRInterval = 100*(amostras.Length-d1)/amostras.Length;
			PreviousRRInterval = 100*d1/amostras.Length;
			CalculaParametrosSimples(amostras, d1);
		
			this.toString();
			}
		
		public void CalculaParametrosSimples(int[] amostras, int d1)
		{
			int l1 = d1;
			int l2 = amostras.Length-d1;			
			
			int baseSample = amostras[2*(d1)/5];
			//int baseSample = 0;
			
			x1 = amostras[(int)(d1-1*l1/5)]-baseSample;
			x2 = amostras[(int)(d1-0.18*l1)]-baseSample;
			x3 = amostras[(int)(d1-0.02*l1)]-baseSample;
			x4 = amostras[(int)(d1-0.01*l1)]-baseSample;
			x5 = amostras[d1]-baseSample;
			x6 = amostras[(int)(d1+0.01*l2)]-baseSample;
			x7 = amostras[(int)(d1+0.02*l2)]-baseSample;
			x8 = amostras[(int)(d1+0.163*l2)]-baseSample;
			x9 = amostras[(int)(d1+0.267*l2)]-baseSample;
			x10 = amostras[(int)(d1+0.82*l2)]-baseSample;
			x11 = amostras[(int)(d1+3*l2/5)]-baseSample;
			
			
		}
		
		
		
		public void comparaMax(BeatParameters beat)
		{
			if(beat.Area > Area) Area = beat.Area;
			if(beat.Integral > Integral) Integral = beat.Integral;
			if(beat.Maximum > Maximum) Maximum = beat.Maximum;
			if(beat.Minimum > Minimum) Minimum = beat.Minimum;
			if(beat.Offset> Offset) Offset = beat.Offset;
			if(beat.Onset > Onset) Onset = beat.Onset;
			if(beat.Offset > Offset) Offset = beat.Offset;
			if(beat.PreviousRRInterval > PreviousRRInterval) PreviousRRInterval = beat.PreviousRRInterval;
			if(beat.QRSDuration > QRSDuration) QRSDuration = beat.QRSDuration;
			if(beat.RRInterval > RRInterval) RRInterval = beat.RRInterval;
			if(beat.x1 > x1) x1 = beat.x1;
			if(beat.x2 > x2) x2= beat.x2;
			if(beat.x3 > x3) x3 = beat.x3;
			if(beat.x4 > x4) x4 = beat.x4;
			if(beat.x5 > x5) x5 = beat.x5;
			if(beat.x6 > x6) x6 = beat.x6;
			if(beat.x7 > x7) x7 = beat.x7;
			if(beat.x8 > x8) x8 = beat.x8;
			if(beat.x9 > x9) x9 = beat.x9;
			if(beat.x10 > x10) x10 = beat.x10;
			if(beat.x11 > x11) x11 = beat.x11;
			
		}
		
		public void comparaMin(BeatParameters beat)
		{
			if(beat.Area < Area) Area = beat.Area;
			if(beat.Integral < Integral) Integral = beat.Integral;
			if(beat.Maximum < Maximum) Maximum = beat.Maximum;
			if(beat.Minimum < Minimum) Minimum = beat.Minimum;
			if(beat.Offset< Offset) Offset = beat.Offset;
			if(beat.Onset < Onset) Onset = beat.Onset;
			if(beat.Offset < Offset) Offset = beat.Offset;
			if(beat.PreviousRRInterval < PreviousRRInterval) PreviousRRInterval = beat.PreviousRRInterval;
			if(beat.QRSDuration < QRSDuration) QRSDuration = beat.QRSDuration;
			if(beat.RRInterval < RRInterval) RRInterval = beat.RRInterval;
			if(beat.x1 < x1) x1 = beat.x1;
			if(beat.x2 < x2) x2= beat.x2;
			if(beat.x3 < x3) x3 = beat.x3;
			if(beat.x4 < x4) x4 = beat.x4;
			if(beat.x5 < x5) x5 = beat.x5;
			if(beat.x6 < x6) x6 = beat.x6;
			if(beat.x7 < x7) x7 = beat.x7;
			if(beat.x8 < x9) x9 = beat.x9;
			if(beat.x10 < x10) x10 = beat.x10;
			if(beat.x11 < x11) x11 = beat.x11;
			
		}
		
		public void geraMedia(BeatParameters beat)
		{
			count++;
			QRSDuration += (beat.QRSDuration-QRSDuration)/count;
			Offset+= (beat.Offset-Offset)/count;
			Onset+= (beat.Onset-Onset)/count;
			Area+= (beat.Area-Area)/count;
			Integral+= (beat.Integral-Integral)/count;
			Maximum+= (beat.Maximum-Maximum)/count;
			Minimum+= (beat.Minimum-Minimum)/count;
			RRInterval+= (beat.RRInterval-RRInterval)/count;
			PreviousRRInterval+= (beat.PreviousRRInterval-PreviousRRInterval)/count;
			x1+=( beat.x1-x1)/count;
			x2+=( beat.x2-x2)/count;
			x3+=( beat.x3-x3)/count;
			x4+=( beat.x4-x4)/count;
			x5+=( beat.x5-x5)/count;
			x6+=( beat.x6-x6)/count;
			x7+=( beat.x7-x7)/count;
			x8+=( beat.x8-x8)/count;
			x9+=( beat.x9-x9)/count;
			x10+=( beat.x10-x10)/count;
			x11=( beat.x11-x11)/count;
		}
		
		public void toString()
		{
		Console.Write("x1:"+x1.ToString("D3")+"\tx2:"+x2.ToString("D3")+"\tx3:"+x3.ToString("D3")+"\tx4:"+x4.ToString("D3")+"\tx5:"+x5.ToString("D3")+"\tx6:"+x6.ToString("D3")
			              +"\tx7:"+x7.ToString("D3")+"\tx8:"+x8.ToString("D3")+"\tx9:"+x9.ToString("D3")+"\tx10:"+x10.ToString("D3")+"\tx11:"+x11.ToString("D3")
			              +"\tmax:"+Maximum.ToString()+"\tmin:"+Minimum.ToString()+"\tqrsD:"+QRSDuration.ToString()+"\tarea:"+Area.ToString()
			              +"\tint:"+Integral.ToString("D6")+"\tRR:"+RRInterval.ToString()+"\tPRR:"+PreviousRRInterval.ToString()+"\n");
		}
	}
}
